{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-16T05:51:28.413726Z",
     "start_time": "2025-01-16T05:51:28.397727Z"
    }
   },
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 示例数据\n",
    "data = [[1], [2], [3], [5], [5]]\n",
    "\n",
    "# 创建StandardScaler对象\n",
    "scaler = StandardScaler()\n",
    "\n",
    "# 拟合数据\n",
    "scaler.fit(data)\n",
    "\n",
    "# 打印均值和标准差\n",
    "print(\"均值:\", scaler.mean_)\n",
    "print(\"标准差:\", scaler.scale_)\n",
    "print(\"方差:\", scaler.var_)\n",
    "\n",
    "# 转换数据\n",
    "scaled_data = scaler.transform(data)\n",
    "print(\"标准化后的数据:\\n\", scaled_data)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值: [3.2]\n",
      "标准差: [1.6]\n",
      "方差: [2.56]\n",
      "标准化后的数据:\n",
      " [[-1.375]\n",
      " [-0.75 ]\n",
      " [-0.125]\n",
      " [ 1.125]\n",
      " [ 1.125]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-16T05:51:28.618283Z",
     "start_time": "2025-01-16T05:51:28.606284Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 示例数据\n",
    "data = [[1], [2], [3], [5], [50001]]\n",
    "\n",
    "# 创建StandardScaler对象\n",
    "scaler = StandardScaler()\n",
    "\n",
    "# 拟合数据\n",
    "scaler.fit(data)\n",
    "\n",
    "# 打印均值、标准差和方差\n",
    "print(\"均值:\", scaler.mean_)\n",
    "print(\"标准差:\", scaler.scale_)\n",
    "print(\"方差:\", scaler.var_)\n",
    "\n",
    "# 转换数据\n",
    "scaled_data = scaler.transform(data)\n",
    "print(\"标准化后的数据:\\n\", scaled_data)"
   ],
   "id": "55b0410e39df8a05",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值: [10002.4]\n",
      "标准差: [19999.30004375]\n",
      "方差: [3.99972002e+08]\n",
      "标准化后的数据:\n",
      " [[-0.5000875 ]\n",
      " [-0.5000375 ]\n",
      " [-0.4999875 ]\n",
      " [-0.49988749]\n",
      " [ 2.        ]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T16:10:34.012064Z",
     "start_time": "2025-07-03T16:10:33.999018Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
    "\n",
    "# ---------------------- 定义原始数据与含离群值数据 ----------------------\n",
    "# 原始收入数据（无离群值）\n",
    "data_normal = np.array([10000, 50000, 80000, 100000]).reshape(-1, 1)  # 形状为(4,1)\n",
    "# 加入离群值后的数据（新增1000000）\n",
    "data_outlier = np.array([10000, 50000, 80000, 100000, 1000000]).reshape(-1, 1)  # 形状为(5,1)\n",
    "\n",
    "# 目标值\n",
    "target_value = 50000\n",
    "# ---------------------- 定义对比函数 ----------------------\n",
    "# 提取目标值在数据中的索引（假设目标值在第一个数据的第二个样本）\n",
    "idx_normal = np.where(data_normal.flatten() == target_value)[0][0]\n",
    "idx_outlier = np.where(data_outlier.flatten() == target_value)[0][0]\n",
    "\n",
    "# ---------------------- 最小最大归一化 ----------------------\n",
    "minmax_scaler = MinMaxScaler()\n",
    "# 无离群值时的归一化结果\n",
    "normal_minmax = minmax_scaler.fit_transform(data_normal)\n",
    "# 有离群值时的归一化结果\n",
    "outlier_minmax = minmax_scaler.fit_transform(data_outlier)\n",
    "\n",
    "# ---------------------- 方差归一化（Z-Score） ----------------------\n",
    "zscore_scaler = StandardScaler()\n",
    "# 无离群值时的归一化结果\n",
    "normal_zscore = zscore_scaler.fit_transform(data_normal)\n",
    "mean_normal_zscore = zscore_scaler.mean_\n",
    "scale_normal_zscore = zscore_scaler.scale_\n",
    "# 有离群值时的归一化结果\n",
    "outlier_zscore = zscore_scaler.fit_transform(data_outlier)\n",
    "mean_outlier_zscore = zscore_scaler.mean_\n",
    "scale_outlier_zscore = zscore_scaler.scale_\n",
    "# ---------------------- 提取目标值的归一化结果 ----------------------\n",
    "# 最小最大归一化结果\n",
    "minmax_normal = normal_minmax[idx_normal][0]\n",
    "minmax_outlier = outlier_minmax[idx_outlier][0]\n",
    "# 方差归一化结果\n",
    "zscore_normal = normal_zscore[idx_normal][0]\n",
    "zscore_outlier = outlier_zscore[idx_outlier][0]\n",
    "\n",
    "# ---------------------- 打印对比结果 ----------------------\n",
    "print(f\"目标值：{target_value}元\")\n",
    "print(\"\\n---------------------- 最小最大归一化对比 ----------------------\")\n",
    "print(f\"无离群值时归一化值：{minmax_normal:.2f}\")\n",
    "print(f\"有离群值时归一化值：{minmax_outlier:.2f}\")\n",
    "print(f\"变化幅度：{np.abs(minmax_outlier - minmax_normal):.2f}\")\n",
    "\n",
    "print(\"\\n---------------------- 方差归一化对比 ----------------------\")\n",
    "print(f\"无离群值时均值：{mean_normal_zscore[0]:.2f}\")\n",
    "print(f\"无离群值时标准差：{scale_normal_zscore[0]:.2f}\")\n",
    "print(f\"无离群值时归一化值：{zscore_normal:.2f}\")\n",
    "\n",
    "\n",
    "print(f\"有离群值时均值：{mean_outlier_zscore[0]:.2f}\")\n",
    "print(f\"有离群值时标准差：{scale_outlier_zscore[0]:.2f}\")\n",
    "print(f\"有离群值时归一化值：{zscore_outlier:.2f}\")\n",
    "\n",
    "print(f\"变化幅度：{np.abs(zscore_outlier - zscore_normal):.2f}\")\n",
    "\n"
   ],
   "id": "3b3a070fddd5bc30",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标值：50000元\n",
      "\n",
      "---------------------- 最小最大归一化对比 ----------------------\n",
      "无离群值时归一化值：0.44\n",
      "有离群值时归一化值：0.04\n",
      "变化幅度：0.40\n",
      "\n",
      "---------------------- 方差归一化对比 ----------------------\n",
      "无离群值时均值：60000.00\n",
      "无离群值时标准差：33911.65\n",
      "无离群值时归一化值：-0.29\n",
      "有离群值时均值：248000.00\n",
      "有离群值时标准差：377221.42\n",
      "有离群值时归一化值：-0.52\n",
      "变化幅度：0.23\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T16:09:47.243259Z",
     "start_time": "2025-07-03T16:09:47.228207Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "9e249835adfef44e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "f3c54241d3847852"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "99ba4c4e8eeeb49a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T15:41:33.082148Z",
     "start_time": "2025-07-04T15:41:33.079138Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "78055ad76614d4f0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T12:08:37.183629Z",
     "start_time": "2025-07-04T12:08:37.178612Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "f0bbfe68b60c31a1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T16:42:25.332722Z",
     "start_time": "2025-07-03T16:42:25.321684Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "3b5710c89825fb3e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T16:42:25.503579Z",
     "start_time": "2025-07-03T16:42:25.493544Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "628af126aebb1cd7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T15:41:25.965339Z",
     "start_time": "2025-07-04T15:41:25.951291Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "7f0bdcf1276d6ada",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T15:05:03.705248Z",
     "start_time": "2025-07-04T15:05:03.693126Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "305ab61bf6720b5f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T15:05:04.049155Z",
     "start_time": "2025-07-04T15:05:04.034103Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "21c2bbba6c8fb96e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T14:55:14.739538Z",
     "start_time": "2025-07-04T14:55:14.736527Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "778fe7f9b0c8e53d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T14:55:14.942240Z",
     "start_time": "2025-07-04T14:55:14.930199Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "f33032202baae14e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T14:57:34.270788Z",
     "start_time": "2025-07-04T14:57:34.263764Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "41bdd844214dc756",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "7c12a05df2ad41d9"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
